package com.igoal.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Type;

@Entity
@Table(name = "user_thumb")
public class UserThumb extends BaseObject implements Audit {
	private static final long serialVersionUID = 6978675713040926215L;
	
	private Long id;
	private ComponentEnum component;
	private Long componentId;
	private User user;
	private boolean up;
	private Date createDate;
	private Date updateDate;
	private DataStatus status;
	
	@Override
	public boolean equals(Object o) {
		if (o == null)
			return false;

		UserThumb ul = (UserThumb) o;
		try {
			if (this.id == ul.id && component.equals(ul.component)
					&& componentId.equals(ul.componentId)
					&& user.equals(ul.user))
				return true;
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return user.hashCode() + id.hashCode() + component.hashCode()
				+ componentId.hashCode();
	}

	@Override
	public String toString() {
		return "User " + user.toString() + " likes " + component;
	}
	
	public UserThumb(){}
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@Enumerated(EnumType.STRING)
	@Column(name="component", nullable=false)
	public ComponentEnum getComponent() {
		return component;
	}

	public void setComponent(ComponentEnum component) {
		this.component = component;
	}

	@Column(name="component_id", nullable=false)
	public Long getComponentId() {
		return componentId;
	}

	public void setComponentId(Long componentId) {
		this.componentId = componentId;
	}
	
	@ManyToOne
	@JoinColumn(name="user_id")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {
		return createDate;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		updateDate = date;
	}
	
	@Column(name = "up", nullable = false)
	@Type(type = "yes_no")
	public boolean isUp() {
		return up;
	}

	public void setUp(boolean up) {
		this.up = up;
	}
}
